<template>
  <div>
    <div class="details" v-for="(item, index) in listData" :key="index">
      <!--判断 单图片 images.length >= 1-->
      <div
        class="sinNews"
        @click="toPostDetail(item.id)"
        v-if="item.images.length < 3"
      >
        <!-- 图片 -->
        <div class="left">
          <img v-lazy="item.images[0]" alt="" />
        </div>

        <!-- 详情 -->
        <div class="right">
          <div class="title">
            <span>{{ item.title }}</span>
          </div>
          <!-- 详情内容 -->
          <div class="summary" v-html="item.summary">
            <!-- {{ item.summary }} -->
          </div>
          <!-- 图标 -->
          <div class="info">
            <div class="lefts">
              <i class="el-icon-location-outline"></i> {{ item.cityName }}
              by
              <img
                v-lazy="$axios.defaults.baseURL + item.account.defaultAvatar"
                alt=""
              />
              <i class="usernickname" @click="$router.push('/user/personal')">{{
                item.account.nickname
              }}</i>
              <i class="el-icon-view"></i>
              {{ item.watch }}
            </div>
            <div class="goods">{{ item.like }}赞</div>
          </div>
        </div>
      </div>

      <!--判断 多图片 -->
      <div
        class="mulNews"
        @click="toPostDetail(item.id)"
        v-if="item.images.length >= 3"
      >
        <!-- 详情 -->
        <div class="title">
          <span>{{ item.title }}</span>
        </div>
        <!-- 详情内容 -->
        <div class="summary" v-html="item.summary">
          <!-- {{ item.summary }} -->
        </div>
        <!-- 图片 -->
        <div class="images">
          <img v-lazy="item.images[0]" alt="" />
          <img v-lazy="item.images[1]" alt="" />
          <img v-lazy="item.images[2]" alt="" />
        </div>
        <!-- 图标 -->
        <div class="info">
          <div class="lefts">
            <i class="el-icon-location-outline"></i> {{ item.cityName }}
            by
            <img
              v-lazy="$axios.defaults.baseURL + item.account.defaultAvatar"
              alt=""
            />
            <i class="usernickname" @click="$router.push('/user/personal')">{{
              item.account.nickname
            }}</i>
            <i class="el-icon-view"></i>
            {{ item.watch }}
          </div>
          <div class="goods">{{ item.like }}赞</div>
        </div>
      </div>
    </div>

    <!-- 分页组件 -->
    <el-pagination
      v-if="postList"
      layout="total, sizes, prev, pager, next, jumper"
      :page-size="pageSize"
      :current-page="pageIndex"
      :page-sizes="[2, 3, 5, 10, 20, 50]"
      :total="postList.length"
      @current-change="currentChange"
      @size-change="sizeChange"
      class="fenye"
    />
  </div>
</template>

<script>
export default {
  data() {
    return {
      postList: [],
      listData: [],
      pageIndex: 1,
      pageSize: 3,
    };
  },
  created() {
    this.loadPost();
  },
  watch: {
    $route: {
      handler() {
        if (this.$route.path === "/post") {
          this.$axios({
            url: "/posts",
            params: {
              city: this.$route.query.city,
            },
          }).then((res) => {
            this.postList = res.data.data;
            this.changePage();
          });
        }
      },
      //不管有没有获取路由都执行一边axios
      immediate: true,
    },
  },
  methods: {
    toPostDetail(id) {
      this.$router.push("/post/detail?id=" + id);
    },
    loadPost() {
      this.$axios({
        url: "/posts",
      }).then((res) => {
        this.postList = res.data.data;
        this.changePage();
      });
    },
    sizeChange(newPageSize) {
      this.pageSize = newPageSize;
      this.changePage();
    },

    currentChange(newPageIndex) {
      this.pageIndex = newPageIndex;
      this.changePage();
    },
    changePage() {
      let start = (this.pageIndex - 1) * this.pageSize;
      let end = this.pageSize + start;
      this.listData = this.postList.slice(start, end);
    },
  },
};
</script>

<style lang="less" scoped>
.details {
  width: 700px;
}

.sinNews,
.mulNews {
  width: 700px;
  padding: 20px 10px;
  border-bottom: 1px solid #ccc;
  cursor: pointer;
  .title {
    width: 460px;
    font-size: 18px;
    color: #000;
    &:hover {
      color: #ffa500;
    }
  }
  .summary {
    font-size: 14px;
    color: #666;
    width: 100%;
    height: 60px;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
  }
  .info {
    display: flex;
    justify-content: space-between;
    font-size: 12px;
    color: #999;
    height: 26px;
    line-height: 26px;
    .lefts {
      img {
        width: 16px;
        height: 16px;
      }
      .usernickname {
        color: #ffa500;
      }
    }
    .goods {
      font-size: 16px;
      color: #ffa500;
    }
  }
}

.sinNews {
  height: 150px;
  display: flex;
  .left {
    max-width: 220px;
    min-width: 220px;
    height: 150px;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .right {
    flex: 1;
    margin-left: 15px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    .title {
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
  }
}

.mulNews {
  .summary {
    margin-top: 10px;
  }
  .images {
    display: flex;
    justify-content: space-around;
    img {
      width: 220px;
      height: 150px;
      margin: 15px 0;
      object-fit: cover;
    }
  }
}

.fenye {
  margin: 15px 0;
}
</style>